package com.zs4sdk.activity;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.zs4sdk.Zs4GameBase;
import com.zs4sdk.Zs4GameMenu;
import com.zs4sdk.common.Extra;
import com.zs4sdk.extra.Zs4GameCode;
import com.common.library.util.ActivityHelper;
import com.common.library.util.Helper;

/**
 * 
 * function: 抽象Activity基类
 * 
 * @ author: aXiBa
 */
public abstract class Zs4GameBaseActivity extends Activity implements OnClickListener{

	private static final String TAG = Zs4GameBaseActivity.class.getSimpleName();
	
	protected static final int TYPE_LEFTBTN_BACKTOMENU = 11;
	protected static final int TYPE_LEFTBTN_BACK = 12;
	protected static final int TYPE_LEFTBTN_BACKTOGAME = 13;
	protected static final int TYPE_LEFTBTN_INVISIBLE = 14;
	protected static final int TYPE_LEFTBTN_OTHER = 15;
	
	protected static final int TYPE_RIGHTBTN_BACKTOGMAGE = 21;
	protected static final int TYPE_RIGHTBTN_INVISIBLE = 22;
	protected static final int TYPE_RIGHTBTN_OTHER = 23;

	protected int mScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
	protected String flag = "";
	
	protected ImageView mLeftView;
	protected Button mRightView;
	int type_leftbtn = TYPE_LEFTBTN_INVISIBLE;
	int type_rightbtn = TYPE_RIGHTBTN_INVISIBLE;

	int ViewHight = 0;
	int ViewWight = 0;

    /**关闭Activity广播**/
	BroadcastReceiver closeReceiver = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		// 设置屏幕方向
		setScreenOrientation();
		overridePendingTransition(0,0);

		// 设置全屏 ,无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
                WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        
        Intent intent = this.getIntent();
		if (Helper.isNotNull(intent) && intent.hasExtra(Extra.INTENTKEY.FLAG)){
			flag = intent.getStringExtra(Extra.INTENTKEY.FLAG);
		}

		if(Helper.isEmpty(flag)) {
			Bundle bundle = intent.getExtras();
			if(Helper.isNotNull(bundle)&&bundle.containsKey(Extra.INTENTKEY.FLAG)) {
				flag = bundle.getString(Extra.INTENTKEY.FLAG);
			}
		}

		super.onCreate(savedInstanceState);
		
		if (Helper.isNull(ActivityHelper.getGlobalApplicationContext())){
			ActivityHelper.init(getApplicationContext());
		}
		
		// 注册关闭监听
		closeReceiver = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				if(Helper.isNotNull(intent) && intent.hasExtra(Extra.INTENTKEY.CLOSEFLAG)) {
					String closeAction = intent.getStringExtra(Extra.INTENTKEY.CLOSEFLAG);
					if(Helper.isNotEmpty(flag) || (Helper.isEmpty(flag) && closeAction.equals(flag))) {
						finish();
					}
				}
			}
		};

		IntentFilter filter = new IntentFilter(Zs4GameBase.getPackageName().concat(Extra.ACTIONKEY.CLOSEACTIVITY_POSTFIX_MENU));
		registerReceiver(closeReceiver, filter);

		DisplayMetrics dm = getResources().getDisplayMetrics();
		int w = dm.widthPixels;
		int h = dm.heightPixels;
		if(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE == Zs4GameBase.getInstance().getScreen_orientation()||
				ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE == Zs4GameBase.getInstance().getScreen_orientation()){
			if(w>h){
			ViewWight = w;
			ViewHight = h;}
			else {
				ViewWight = h;
				ViewHight = w;
			}
		}else {
			if(w>h){
				ViewWight = h;
				ViewHight = w;}
			else {
				ViewWight = w;
				ViewHight = h;
			}
		}


	}

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
	    super.onPause();
	}
	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		if(Helper.isNotNull(closeReceiver)) {
			unregisterReceiver(closeReceiver);
		}
	}

	
	/**
	 * 根据指定View类型及id取得相应的控件
	 * @param clazz 所要取得的控件类型,
	 * @param resId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected final <T extends View> T findView(Class<T> clazz, int resId){
		return (T)this.findViewById(resId);
	}
	/**
	 * 根据指定View类型及id取得相应的控件
	 * @param clazz 所要取得的控件类型,
	 * @param resId 名称,即R.string.之后的部分
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected final <T extends View> T findView(Class<T> clazz, String resId){
		return (T)this.findViewById(ActivityHelper.getIdResId(resId));
	}
	/**
	 * 控制指定view的显示状态
	 * @param visiblity 是否显示(可取值有View.VISIBLE, View.GONE, View.INVISIBLE)
	 * @param viewIds
	 */
	protected final void visibleViews(int visiblity, int... viewIds){
		ActivityHelper.visibleViews(this, visiblity, viewIds);
	}
	/**
	 * 控制指定view的显示状态
	 * @param visiblity 是否显示(可取值有View.VISIBLE, View.GONE, View.INVISIBLE)
	 * @param viewIds
	 */
	protected final void visibleViews(int visiblity, String... viewIds){
		ActivityHelper.visibleViews(this, visiblity, viewIds);
	}
	
	/**
	 * 控制指定view的显示状态
	 * @param visiblity 是否显示(可取值有View.VISIBLE, View.GONE, View.INVISIBLE)
	 * @param views
	 */
	protected final void visibleViews(int visiblity, View... views){
		ActivityHelper.visibleViews(visiblity, views);
	}
//	/**
//	 * 跳转到指定Activity(不结束当前的)
//	 * @param toActivity
//	 */
//	protected final void switchTo(Class<?> toActivity){
//		ActivityHelper.switchTo(this, toActivity);
//	}
//	protected final void switchTo(Class<?> toActivity, Bundle bundle){
//		ActivityHelper.switchTo(this, toActivity, bundle);
//	}
//	/**
//	 * 跳转到指定Activity(不结束当前的)
//	 * @param toActivity
//	 * @param enterAnim 进入动画
//	 * @param exitAnim 退出动画
//	 */
//	protected final void switchTo(Class<?> toActivity, int enterAnim, int exitAnim) {
//		ActivityHelper.switchTo(this, toActivity, false, enterAnim, exitAnim);
//	}
//	/**
//	 * 跳转到指定Activity
//	 * @param toActivity
//	 * @param finish 是否结束当前Activity
//	 */
//	protected final void switchTo(Class<?> toActivity, boolean finish){
//		ActivityHelper.switchTo(this, toActivity, finish, 0, 0);
//	}
//	/**
//	 * 跳转到指定Activity
//	 * @param toActivity
//	 * @param finish 是否结束当前Activity
//	 * @param enterAnim 进入动画
//	 * @param exitAnim 退出动画
//	 */
//	protected final void switchTo(Class<?> toActivity, boolean finish, int enterAnim, int exitAnim){
//		ActivityHelper.switchTo(this, toActivity, finish, enterAnim, exitAnim);
//	}
	/**
	 * 设置指定View的监听
	 * @param listener
	 * @param viewIds
	 */
	protected final void setViewClickEvent(OnClickListener listener, int... viewIds){
		ActivityHelper.setViewClickEvent(this, listener, viewIds);
	}
	/**
	 * 设置指定View的监听
	 * @param listener
	 * @param viewIds
	 */
	protected final void setViewClickEvent(OnClickListener listener, String... viewIds){
		ActivityHelper.setViewClickEvent(this, listener, viewIds);
	}
	
	
	// 是否返回菜单
	protected int getLeftBtnType() {
		if(Extra.FLAG_MENU.equals(flag) || Extra.FLAG_MANUAL_MENU.equals(flag)) {
			return TYPE_LEFTBTN_BACKTOMENU;
		}else if(Extra.FLAG_MENU_ACCOUNT.equals(flag)){
			return TYPE_LEFTBTN_BACKTOGAME;
		}
		else {
			return TYPE_LEFTBTN_BACK;
		}
	}
	
	// 是否返回游戏
	protected int getRightBtnType() {
		if(Extra.FLAG_MENU.equals(flag) || Extra.FLAG_MANUAL_MENU.equals(flag)) {
			return TYPE_RIGHTBTN_BACKTOGMAGE;
		}
		else {
			return TYPE_RIGHTBTN_INVISIBLE;
		}
	}
//
//	@Override
//	protected void onRestart() {
//		super.onRestart();
//		Zs4GameMenu.startService(this, this.getApplicationContext().getPackageName());
//		Zs4GameBase.getInstance().hiddenLogoButton(this);
//	}
	
	
	/**
	 * 初始化
	 */
	protected void init() {
		this.mLeftView = this.findView(ImageView.class, ActivityHelper.getIdResId("zs4gameviewid_mLeftView_view_titlebar"));
		this.mRightView = this.findView(Button.class, ActivityHelper.getIdResId("zs4gameviewid_mRightView_view_titlebar"));
		
		type_leftbtn = getLeftBtnType();
		if(TYPE_LEFTBTN_BACKTOMENU == type_leftbtn) {
//			this.mLeftView.setText(ActivityHelper.getStringResId("zs4gamestring_menu_btn_menu"));
		}
		else if(TYPE_LEFTBTN_BACK == type_leftbtn || TYPE_LEFTBTN_BACKTOGAME == type_leftbtn) {
//			this.mLeftView.setText(ActivityHelper.getStringResId("zs4gamestring_btn_back"));
		}
		else if(TYPE_LEFTBTN_INVISIBLE == type_leftbtn) {
			this.mLeftView.setVisibility(View.GONE);
		}
		
		type_rightbtn = getRightBtnType();
		if(TYPE_RIGHTBTN_BACKTOGMAGE == type_rightbtn) {
			this.mRightView.setText(ActivityHelper.getStringResId("zs4gamestring_menu_btn_backgame"));
		}
		else if(TYPE_RIGHTBTN_INVISIBLE == type_rightbtn) {
			mRightView.setVisibility(View.GONE);
		}
		
		this.mLeftView.setOnClickListener(this);
		this.mRightView.setOnClickListener(this);
	}

	
	@Override
	public void onClick(View v) {
		if (v.getId() == ActivityHelper.getIdResId("zs4gameviewid_mLeftView_view_titlebar")) {
			backHandle();
		}
		else if(v.getId() == ActivityHelper.getIdResId("zs4gameviewid_mRightView_view_titlebar")) {
			if(TYPE_RIGHTBTN_BACKTOGMAGE == type_rightbtn) {
				backToGame();
			}
		}
	}

	
	/**
	 * 
	 * function: 回到菜单
	 *
	 * 
	 * @ author: aXiBa 2013-3-23 下午4:57:26
	 */
	protected void backToMenu() {
		
		if(Extra.FLAG_MENU.equals(flag) || Extra.FLAG_MANUAL_MENU.equals(flag)||Extra.FLAG_MENU_ACCOUNT.equals(flag)) {
			Zs4GameMenu.showGridMenu(this, flag);

			ActivityHelper.sendBroadcast(Zs4GameBase.getPackageName().concat(Extra.ACTIONKEY.MESSAGE_POSTFIX), Zs4GameCode.BACKTOMENU, this);
		}
		
		finish();
	}

	/**
	 * 
	 * function: 回到游戏
	 *
	 * 
	 * @ author: aXiBa 2013-3-23 下午4:57:33
	 */
	protected void backToGame() {
		ActivityHelper.sendBroadcast(Zs4GameBase.getPackageName().concat(Extra.ACTIONKEY.MESSAGE_POSTFIX), Zs4GameCode.BACKTOGAME, this);
		Zs4GameMenu.closeActivitysAndShowStartMenu(this, flag);
	}
	
	
	@Override
	public void onBackPressed() {
		
		backHandle();
	}
	

	/**
	 * 
	 * function: 后退处理
	 *
	 * 
	 * @ author: aXiBa 2013-12-15 下午8:23:47
	 */
	private void backHandle() {
		
		if(TYPE_LEFTBTN_BACKTOMENU == type_leftbtn) {
			backToMenu();
		}
		else if(TYPE_LEFTBTN_BACK == type_leftbtn) {
			setResult(RESULT_OK);
			finish();
		}
		else if(TYPE_LEFTBTN_BACKTOGAME == type_leftbtn) {
			ActivityHelper.sendBroadcast(flag, Zs4GameCode.BACKTOGAME, this);
			Zs4GameMenu.closeActivitysAndShowStartMenu(this, flag);
		}
	}
	
	/**
	 * 设置顶部左边按键
	 * @param show 是否显示
	 * @param textResId 显示的文字
	 */
	protected void setTopLeftButton(boolean show, int textResId){
		this.mLeftView.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
		if (show){
//			this.mLeftView.setText(textResId);
		}
	}
	/**
	 * 设置顶部右边按键
	 * @param show 是否显示
	 * @param textResId 显示的文字
	 */
	protected void setTopRightButton(boolean show, int textResId){
		this.mRightView.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
		if (show){
			this.mRightView.setText(textResId);
		}
	}
	/**
	 * 设置标题
	 * @param titleRes
	 */
	protected final void setTopTitle(int titleRes){
		TextView txvTitle = findView(TextView.class, ActivityHelper.getIdResId("zs4gameviewid_mTextView_view_titlebar"));
		if(Helper.isNotNull(txvTitle)){
			txvTitle.setText(titleRes);
		}
	}
	protected final void setTopTitle(String titleRes){
		TextView txvTitle = findView(TextView.class, ActivityHelper.getIdResId("zs4gameviewid_mTextView_view_titlebar"));
		if(Helper.isNotNull(txvTitle)){
			txvTitle.setText(titleRes);
		}
	}


	/**
	 * 
	 * function: 设置屏幕方向
	 *
	 * 
	 * @ author: aXiBa 2013-3-21 上午10:17:08
	 */
	private void setScreenOrientation() {
		
		Intent intent = this.getIntent();
		if (Helper.isNotNull(intent)){
			this.mScreenOrientation = intent.getIntExtra(Extra.INTENTKEY.SCREEN_ORIENTATION, -1);
		}
		
		Bundle bundle = intent.getExtras();
		if (Helper.isNotNull(bundle)){
			this.mScreenOrientation = bundle.getInt(Extra.INTENTKEY.SCREEN_ORIENTATION, -1);
		}
		
		if(-1 == mScreenOrientation) {
			mScreenOrientation = Zs4GameBase.getInstance().getScreen_orientation();
		}

		setRequestedOrientation(this.mScreenOrientation);
	}
	
}
